import Vue from 'vue';
import Router from 'vue-router';
import store from './store/index'
import { getOauthInit} from './api/permission'
import config from '@/config/baseUrl'
Vue.use(Router);

const routes = [
  {
    path: '*',
    component: () => import('./view/index'),
    redirect: '/index'
  },
  {
    name: 'index',
    component: () => import('./view/index'),
    meta: {
      title: '首页',
	  keepAlive:true
    }
  },
  {
    name: 'user',
    component: () => import('./view/user'),
    meta: {
      title: '工会网课',
	  keepAlive:false
    }
  },
  {
    name: 'cart',
    component: () => import('./view/cart'),
    meta: {
      title: '上传视频',
	  keepAlive:false
    }
  },
  {
    name: 'search',
    component: () => import('./view/search'),
    meta: {
      title: '工会网课',
	  keepAlive:false
    }
  },
  {
    name: 'goods',
    component: () => import('./view/goods'),
    meta: {
      title: '视频详情',
	  keepAlive:false
    }
  },
  {
    name: 'goods2',
    component: () => import('./view/goods2'),
    meta: {
      title: '短视频详情',
  	  keepAlive:false
    }
  },
  {
    name: 'game',
    component: () => import('./view/game'),
    meta: {
      title: '活动',
  	  keepAlive:false
    }
  }
];

// add route path
routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '');
});

const router = new Router({base:`${config.baseUrlRouter}`,routes });

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title;
  if (title) {
    document.title = title;
  }
  if(to.name == 'cart'||to.name == 'user'||to.name=='game'){
	  if(to.query.accessToken){
		  store.commit('LOGIN_IN',to.query.accessToken)
	  }
	  if(!store.state.userToken){
		  window.location.href = `${config.reviewUrl + config.backUrl+ to.fullPath}`
	  }else{
		  next()
	  }
  }else{
	  next()
  }
});

export {
  router
};
